﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TimKiem.DataLayer.TimKiem;
using Ext.Net;

namespace TKDVQ10.Search
{
    public partial class SearchService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //You must specify Google Map API Key for this component. You can obtain this key from http://code.google.com/apis/maps/signup.html
                //For samples to run properly, set GoogleAPIKey in Web.Config file.
                GoogleMapForASPNet1.GoogleMapObject.APIKey = ConfigurationManager.AppSettings["GoogleAPIKey"];

                //Specify width and height for map. You can specify either in pixels or in percentage relative to it's container.
                GoogleMapForASPNet1.GoogleMapObject.Width = "700px"; // You can also specify percentage(e.g. 80%) here
                GoogleMapForASPNet1.GoogleMapObject.Height = "400px";

                //Specify initial Zoom level.
                GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 14;

                //Specify Center Point for map. Map will be centered on this point.
                GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("1", 10.768080711364746, 106.66602325439453);

                //Add push pins for map. 
                //This should be done with intialization of GooglePoint class. 
                //ID is to identify a pushpin. It must be unique for each pin. Type is string.
                //Other properties latitude and longitude.
                GooglePoint GP1 = new GooglePoint();
                GP1.ID = "1";
                GP1.Latitude = 10.768080711364746;
                GP1.Longitude = 106.66602325439453;
                //Specify bubble text here. You can use standard HTML tags here.
                GP1.InfoHTML = @"UBND Quận 10, P.14, Q.10,TP Ho Chi Minh, Vietnam";
                //Add tooltip for this point
                GP1.ToolTip = @"UBND Quận 10, P.14, Q.10,TP Ho Chi Minh, Vietnam";
                GP1.Address = "UBND Quận 10, P.14, Q.10,TP Ho Chi Minh, Vietnam";
                //Specify icon image. This should be relative to root folder.
                //GP1.IconImage = "icons/pushpin-blue.png";
                GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP1);
            }
            
        }
    
        public void btnSearch_Clicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cbTuKhoa.SelectedItem.Value.Trim()))
            {
                DataSet ds = TimKiemRepository.TimKiem(cbTuKhoa.SelectedItem.Value.Trim());

                if (ds.Tables.Count > 0 && ds != null)
                {
                    DataTable dt = ds.Tables["Results"];
                    if (dt.Rows.Count > 0)
                    {
                        grdResults.DataSource = dt;
                        grdResults.DataBind();
                    }
                    else
                    {
                        BindData();
                    }
                }
                else
                {
                    BindData();
                }
            }
            
        }
      
        private void BindData()
        {
            String[] SearchArrayList = cbTuKhoa.SelectedItem.Value.Trim().Split(new Char[]
                                                                                {
                                                                                  '+', ' ', ','
                                                                                }
                                                                                , StringSplitOptions.RemoveEmptyEntries);

            String strJoin = string.Join("",SearchArrayList);

            DataSet result = TimKiemRepository.SearchLike(strJoin);
           
                if (result.Tables.Count > 0 && result != null)
                {
                    DataTable dt = result.Tables["Results"];
                    if (dt.Rows.Count > 0)
                    {
                        grdResults.DataSource = dt;
                        grdResults.DataBind();
                    }
                    else
                    {
                        grdResults.EmptyDataText = "<span style='font: inherit;font-weight: bold; color: #222;'> Not found any result for \"" + cbTuKhoa.SelectedItem.Value.Trim() + "\" </span>";
                        grdResults.DataBind();
                    }
                }
                else
                {
                    grdResults.EmptyDataText = "<span style='font: inherit;font-weight: bold; color: #222;'> Not found any result for \"" + cbTuKhoa.SelectedItem.Value.Trim() + "\" </span>";
                    grdResults.DataBind();
                }
            
        }
      
        public void lbtnDiaChi_Clicked(object sender, EventArgs e)
        {
            
            System.Web.UI.WebControls.LinkButton link = sender as System.Web.UI.WebControls.LinkButton;
            int rowIndex = Convert.ToInt32(link.CommandArgument);
            double kinhdo =double.Parse( (grdResults.Rows[rowIndex].FindControl("hidKinhDo") as HiddenField).Value);
            double vido = double.Parse((grdResults.Rows[rowIndex].FindControl("hidViDo") as HiddenField).Value);
           


            //Specify Center Point for map. Map will be centered on this point.
            GoogleMapForASPNet1.GoogleMapObject.CenterPoint = new GooglePoint("1", vido, kinhdo);



            GoogleMapForASPNet1.GoogleMapObject.Points[0].Latitude = vido;
            GoogleMapForASPNet1.GoogleMapObject.Points[0].Longitude = kinhdo;
            //Specify bubble text here. You can use standard HTML tags here.
            GoogleMapForASPNet1.GoogleMapObject.Points[0].InfoHTML = "<span>" + link.Text + "</span>";
            //Add tooltip for this point
            GoogleMapForASPNet1.GoogleMapObject.Points[0].ToolTip =  link.Text ;
            GoogleMapForASPNet1.GoogleMapObject.Points[0].Address =  link.Text;
            GoogleMapForASPNet1.Update();
            //Specify icon image. This should be relative to root folder.
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].IconImage = "icons/3.png";
        }
        protected void RowSelect(object sender, DirectEventArgs e)
        {
            string madulieu = e.ExtraParams["MaDuLieu"];
            string diachi = e.ExtraParams["DiaChi"];
            string kinhdo = e.ExtraParams["KinhDo"];
            string vido = e.ExtraParams["ViDo"];

            GoogleMapForASPNet1.GoogleMapObject.Points[0].Latitude = 10.7811393484449;
            GoogleMapForASPNet1.GoogleMapObject.Points[0].Longitude = 106.669608126731;
            //Specify bubble text here. You can use standard HTML tags here.
            GoogleMapForASPNet1.GoogleMapObject.Points[0].InfoHTML = "<p>" + "quyen" + "</p>";
            //Add tooltip for this point
            GoogleMapForASPNet1.GoogleMapObject.Points[0].ToolTip = "<p>" + "quyen" + "</p>";
            GoogleMapForASPNet1.GoogleMapObject.Points[0].Address = "<p>" + "quyen" + "</p>";
            //Specify icon image. This should be relative to root folder.
            GoogleMapForASPNet1.GoogleMapObject.Points[0].IconImage = "icons/RedCar.png";
         
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].Latitude = double.Parse(kinhdo);
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].Longitude = double.Parse(vido);
            ////Specify bubble text here. You can use standard HTML tags here.
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].InfoHTML = "<p>" + diachi + "</p>";
            ////Add tooltip for this point
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].ToolTip = "<p>" + diachi + "</p>";
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].Address = "<p>" + diachi + "</p>";
            ////Specify icon image. This should be relative to root folder.
            //GoogleMapForASPNet1.GoogleMapObject.Points[0].IconImage = "icons/pushpin-blue.png";

           
        }
    }
}
